package ink.sunflowerk.service.impl;


import ink.sunflowerk.dao.MyDirectoryMapper;
import ink.sunflowerk.entity.MyDirectory;
import ink.sunflowerk.exception.DirectoryException;
import ink.sunflowerk.service.MyDirectoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class MyDirectoryServiceImp  implements MyDirectoryService {
    @Autowired
    private MyDirectoryMapper myDirectoryMapper;

    @Override
    public void addOne(MyDirectory myDirectory) {
        //判断目录是否存在  -- 重复父修改失败
        MyDirectory dbMyDirectory=myDirectoryMapper.getOneByDname(myDirectory.getDname(),myDirectory.getDfu(),myDirectory.getUid());
        if(dbMyDirectory!=null){
            throw new DirectoryException("文件夹/文件名已存在！");
        }
        myDirectoryMapper.addOne(myDirectory);
    }

    @Override
    public void deleteOne(int did) {
          MyDirectory myDirectory=myDirectoryMapper.getOneByDid(did);
          //删除所有的子文件
          myDirectoryMapper.deleteZis(myDirectory.getDfu()+"-"+myDirectory.getDid());
          //删除自己
          myDirectoryMapper.deleteOne(did);

    }

    @Override
    public void updateOne(MyDirectory myDirectory) {
           //保证当前目录下名字是唯一的
            MyDirectory dbMyDirectory=myDirectoryMapper.getOneByDname(myDirectory.getDname(),myDirectory.getDfu(),myDirectory.getUid());
            if(dbMyDirectory!=null&&dbMyDirectory.getDid()!=myDirectory.getDid()){
                 throw new DirectoryException("文件夹/文件名已存在！");
            }
            myDirectoryMapper.updateOne(myDirectory);
    }

    @Override
    public MyDirectory getOneByDid(int did) {
        return myDirectoryMapper.getOneByDid(did);
    }

    @Override
    public List<MyDirectory> getAllByDfu(String dfu, int uid) {
        return myDirectoryMapper.getAllByDfu(dfu,uid);
    }
}
